package sanbing.jcpplearning;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.ByteOrder;

public class ByteBufByteOrderDemo {
    public static void main(String[] args) {
        // 模拟充电桩上传的字节流 0x12 34 56 78
        byte[] data = {0x12, 0x34, 0x56, 0x78};

        // 使用 Netty 的 ByteBuf 包装字节数组
        ByteBuf buffer = Unpooled.wrappedBuffer(data);

        // 设置为大端序读取
        int bigEndianResult = buffer.order(ByteOrder.BIG_ENDIAN).readInt();

        // 重新设置读取索引（因为 ByteBuf 的读操作会移动索引）
        buffer.readerIndex(0);

        // 设置为小端序读取
        int littleEndianResult = buffer.order(ByteOrder.LITTLE_ENDIAN).readInt();

        // 打印结果
        System.out.printf("原始字节流: 0x%02X 0x%02X 0x%02X 0x%02X\n", data[0], data[1], data[2], data[3]);
        System.out.printf("大端序解析结果: 0x%08X (十进制: %d)\n", bigEndianResult, bigEndianResult);
        System.out.printf("小端序解析结果: 0x%08X (十进制: %d)\n", littleEndianResult, littleEndianResult);

        // 释放 ByteBuf
        buffer.release();
    }
}